package com.yunqiao.service.api.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunqiao.common.consts.AppConsts;
import com.yunqiao.common.exception.CustomApiException;
import com.yunqiao.service.api.entity.OrderEntity;
import com.yunqiao.service.api.mapper.OrderMapper;
import com.yunqiao.service.api.vo.EvaluateVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.utils.Query;

import com.yunqiao.service.api.mapper.EvaluateMapper;
import com.yunqiao.service.api.entity.EvaluateEntity;
import com.yunqiao.service.api.EvaluateService;


@Service("evaluateService")
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, EvaluateEntity> implements EvaluateService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private EvaluateMapper evaluateMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //各种查询条件的过滤,在此进行
        QueryWrapper<EvaluateEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("e.deleted", 0);
        if (MapUtil.getInt(params, "targetUserId") != null) {
            queryWrapper.eq("e.target_user_id", MapUtil.getInt(params, "targetUserId"));
        }

        if (MapUtil.getInt(params, "score") != null) {
            queryWrapper.eq("e.score", MapUtil.getInt(params, "score"));
        }
        if (StringUtils.isNotEmpty(MapUtil.getStr(params, "createTime")) && StringUtils.isNotEmpty(MapUtil.getStr(params, "updateTime"))) { //当开始时间用
            queryWrapper.between("e.create_time", DateUtil.parse(MapUtil.getStr(params, "createTime"), "yyyy-MM-dd"), DateUtil.parse(MapUtil.getStr(params, "updateTime"), "yyyy-MM-dd"));
        }
        IPage<EvaluateVo> page = evaluateMapper.PageList(new Query<EvaluateEntity>(params).getPage(),queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public void addEvaluate(EvaluateEntity evaluate) {
        if (evaluate.getOrderId() == null) {
            throw new CustomApiException("订单id不能为空");
        }

        OrderEntity order = orderMapper.selectById(evaluate.getOrderId());
        if (order == null) {
            throw new CustomApiException("没有这个订单");
        }

        if (!order.getStatus().equals(AppConsts.ORDER_STATUS_6) && !order.getStatus().equals(AppConsts.ORDER_STATUS_7) && !order.getStatus().equals(AppConsts.ORDER_STATUS_8)) {
            throw new CustomApiException("这个订单还没有完成");
        }
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("order_id", order.getId());
        qw.eq("type", evaluate.getType());
        if (evaluate.getType().equals(AppConsts.EVALUATE_TYPE_1)) {
            qw.eq("user_id", order.getDriverId());
            qw.eq("target_user_id", order.getUserId());

            evaluate.setUserId(order.getDriverId().intValue());
            evaluate.setTargetUserId(order.getUserId().intValue());
            if (order.getStatus().equals(AppConsts.ORDER_STATUS_7)) {
                order.setStatus(AppConsts.ORDER_STATUS_9);
            } else {
                order.setStatus(AppConsts.ORDER_STATUS_8);
            }
        } else {
            qw.eq("target_user_id", order.getDriverId());
            qw.eq("user_id", order.getUserId());

            evaluate.setTargetUserId(order.getDriverId().intValue());
            evaluate.setUserId(order.getUserId().intValue());
            if (order.getStatus().equals(AppConsts.ORDER_STATUS_8)) {
                order.setStatus(AppConsts.ORDER_STATUS_9);
            } else {
                order.setStatus(AppConsts.ORDER_STATUS_7);
            }
        }

        Integer count = evaluateMapper.selectCount(qw);
        if (count > 0) {
            throw new CustomApiException("您已经评价过了");
        }

        orderMapper.updateById(order);
        evaluateMapper.insert(evaluate);
    }

    @Override
    public Double getScore(Integer targetUserId) {
        //平均分
        Double score = evaluateMapper.getScore(targetUserId);
        BigDecimal b = new BigDecimal(score);
        b = b.setScale(1, BigDecimal.ROUND_HALF_UP);
        return b.doubleValue();
    }

    @Override
    public EvaluateEntity getEvaluate(Long orderId, Long targetUserId) {
        EvaluateEntity evaluateEntity = evaluateMapper.selectOne(new QueryWrapper<EvaluateEntity>().eq("order_id", orderId).eq("target_user_id", targetUserId));
        if (ObjectUtil.isNull(evaluateEntity)) {
            evaluateEntity = new EvaluateEntity();
        }
        return evaluateEntity;
    }

    @Override
    public void deleteState(Long id) {
        EvaluateEntity evaluateEntity = this.evaluateMapper.selectById(id);
        evaluateEntity.setDeleteType(AppConsts.DELETE_TYPE_1);
        evaluateMapper.updateById(evaluateEntity);
    }

    @Override
    public EvaluateVo details(Integer id) {
        EvaluateVo evaluateVo = evaluateMapper.details(id);
        return evaluateVo;
    }

}
